﻿using Hidistro.Entities;
using Hidistro.Entities.Comments;
using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using wnxd.javascript;
using System.IO;
using System.Web.UI;
using System.Reflection;
using JSShop.Model;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class DetailPages_ConsultationList : AscxTemplatedWebControl
    {
        Repeater CList;
        HtmlAnchor LoadMore;
        const int Pager_size = 6;
        int pn = 0;
        protected override void AttachChildControls()
        {
            this.CList = (Repeater)this.FindControl("CList");
            this.LoadMore = (HtmlAnchor)this.FindControl("LoadMore");
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_Comment/DetailPages_ConsultationList_3q.ascx";
            }
            base.OnInit(e);
        }
        public List<Hishop_ProductConsultations> DataSource
        {
            get;
            set;
        }
        private void ControlDataBind()
        {
            if (this.DataSource != null && this.DataSource.Count > 0)
            {
                this.DataSource = this.DataSource.Skip(pn * Pager_size).Take(Pager_size).ToList();
                if (this.DataSource.Count > 0)
                {
                    this.CList.DataSource = this.DataSource;
                    this.CList.DataBind();
                    this.LoadMore.Visible = true;
                }
            }
        }
        public override void DataBind()
        {
            this.EnsureChildControls();
            this.ControlDataBind();
        }
        public json ConsultationListPager()
        {
            json r = new json(new { error = true });
            if (int.TryParse(this.Page.Request.Form["pn"], out this.pn))
            {
                if (this.pn > 0)
                {
                    this.AttachChildControls();
                    this.ControlDataBind();
                    if (this.DataSource != null && this.DataSource.Count > 0)
                    {
                        r["error"] = false;
                        using (StringWriter sw = new StringWriter())
                        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                        {
                            MethodInfo Render = typeof(Repeater).GetMethod("Render", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                            Render.Invoke(this.CList, new object[] { htw });
                            r["html"] = sw.ToString();
                        }
                    }
                }
            }
            return r;
        }
    }
}
